package com.cambright.stack.components
{
	import com.cambright.lotusroot.Command;
	import com.cambright.lotusroot.Component;
	
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.Event;
	
	public class MenuComponent extends Component
	{
		private var _level:$MenuMap;
		private var _root:$MenuMap;
		
		private var _target:NativeMenu;
		public function get target():NativeMenu
		{
			return _target;
		}
		public function set target(value:NativeMenu):void
		{
			_target = value;
		}
		
		override protected function dispose():void
		{
			_level = null;
			_root = null;
			_target = null;
		}
		
		override protected function invoke():void
		{
			_level = _root = new $MenuMap(null);
		}
		
		override protected function enable():void
		{
			_enableMenu(_root, _target)
		}
		
		override protected function disable():void
		{
			_disableMenu(_target);
		}
		
		private function _enableMenu(map:$MenuMap, menu:NativeMenu):void
		{
			for each(var itemMap:$MenuMap in map)
			{
				var item:NativeMenuItem = itemMap.item;
				menu.addItem(item);
				
				if(itemMap.length > 0)
				{
					item.submenu = new NativeMenu();
					_enableMenu(itemMap, item.submenu);
				} 
				else if(!item.isSeparator)
				{
					item.addEventListener(Event.SELECT, _onItemSelected);
				}
			}
		}
		
		private function _disableMenu(menu:NativeMenu):void
		{
			while(menu.items.length > 0)
			{
				var item:NativeMenuItem = menu.getItemAt(0);
				menu.removeItem(item);
				
				if(item.submenu != null)
				{
					_disableMenu(item.submenu);
					item.submenu = null;
				}
				else
				{
					item.removeEventListener(Event.SELECT, _onItemSelected);
				}
			}
		}

		private function _onItemSelected(event:Event):void
		{
			var target:NativeMenuItem = event.currentTarget as NativeMenuItem;
			if(target.data is Function)
			{
				var fun:Function = target.data as Function;
				fun.apply();
			}
			else if(target.data is Class)
			{
				var instance:Object = new target.data();
				
				if(instance is Command)
				{
					var command:Command = instance as Command;
					command.dispatch();
				}
			}
		}
		
		public function addItem(label:String = null, data:Object = null, enabled:Boolean = true):NativeMenuItem
		{
			var itemMap:$MenuMap = new $MenuMap(_level);
			_level.add(itemMap);
			if(label == null || label.length == 0)
				itemMap.item = new NativeMenuItem("", true);
			else
				itemMap.item = new NativeMenuItem(label);
			itemMap.item.enabled = enabled;
			itemMap.item.data = data;
			return itemMap.item;
		}
		
		public function next():void
		{
			if(_level.length > 0)
				_level = _level[_level.length - 1];
		}
		
		public function back():void
		{
			if(_level.owner != null)
				_level = _level.owner;
		}
	}
}
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;

dynamic class $MenuMap extends Array
{
	public var owner:$MenuMap;
	public var item:NativeMenuItem;
	
	public function $MenuMap(owner:$MenuMap)
	{
		this.owner = owner;
	}
	
	public function add(child:$MenuMap):void
	{
		push(child);
	}
}